﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.LIB;

namespace HYElectricSetting
{
	public partial class HYElectricSettingMainForm : System.Windows.Forms.Form
	{
		public HYElectricSettingMainForm(Document doc, string nodeKey)
		{
			this.InitializeComponent();
			base.IsMdiContainer = true;
			this.Doc = doc;
			this.NodeKey = nodeKey;
            this.SubForms = new Dictionary<string, System.Windows.Forms.Form>();
		}

		private void HYElectricSettingMainForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.buttonApply.Enabled = false;
			foreach (object obj in base.Controls)
			{
                System.Windows.Forms.Control control = (System.Windows.Forms.Control)obj;
				try
				{
					MdiClient mdiClient = (MdiClient)control;
					mdiClient.BackColor = this.BackColor;
				}
				catch (InvalidCastException ex)
				{
				}
			}
			this.InitTreeView();
			this.InitFormsContainer();
			this.SelectTreeNode();
		}

		private void InitTreeView()
		{
			this.treeView.ShowNodeToolTips = true;
			this.treeView.Nodes.Clear();
			TreeNode treeNode = this.treeView.Nodes.Add("WireRoot", "配线设置");
			treeNode.Nodes.Add("WireType", "导线类型").ToolTipText = "导线类型";
			treeNode.Nodes.Add("CableManage", "电缆管理").ToolTipText = "电缆管理";
			treeNode = this.treeView.Nodes.Add("ConduitRoot", "线管设置");
			treeNode.Nodes.Add("ConduitType", "线管类型").ToolTipText = "线管类型";
			treeNode.Nodes.Add("ConduitFitting", "线管配件").ToolTipText = "线管配件";
			treeNode = this.treeView.Nodes.Add("WireConduit", "导线穿管");
			treeNode = this.treeView.Nodes.Add("CableTrayRoot", "电缆桥架设置");
			treeNode.Nodes.Add("CableTrayFitting", "电缆桥架配件").ToolTipText = "电缆桥架配件";
			treeNode = this.treeView.Nodes.Add("LightningRoot", "防雷接地");
			treeNode.Nodes.Add("LightningLine", "防雷接地线").ToolTipText = "防雷接地线";
			this.treeView.ExpandAll();
		}

		private void InitFormsContainer()
		{
            System.Windows.Forms.Form form = new WireSizeForm(this.Doc);
			form.MdiParent = this;
			form.Parent = this.panel;
			form.Tag = this;
			this.SubForms.Add("WireRoot", form);
			this.SubForms.Add("WireType", form);
            System.Windows.Forms.Form form2 = new CableManagerForm();
			form2.MdiParent = this;
			form2.Parent = this.panel;
			form2.Tag = this;
			this.SubForms.Add("CableManage", form2);
            System.Windows.Forms.Form form3 = new ConduitSizeForm(this.Doc);
			form3.MdiParent = this;
			form3.Parent = this.panel;
			form3.Tag = this;
			this.SubForms.Add("ConduitRoot", form3);
			this.SubForms.Add("ConduitType", form3);
            System.Windows.Forms.Form form4 = new ConduitTypeForm(this.Doc);
			form4.MdiParent = this;
			form4.Parent = this.panel;
			form4.Tag = this;
			this.SubForms.Add("ConduitFitting", form4);
            System.Windows.Forms.Form form5 = new WireConduitForm(this.Doc);
			form5.MdiParent = this;
			form5.Parent = this.panel;
			form5.Tag = this;
			this.SubForms.Add("WireConduit", form5);
            System.Windows.Forms.Form form6 = new CableTrayFittingSet(this.Doc);
			form6.MdiParent = this;
			form6.Parent = this.panel;
			form6.Tag = this;
			this.SubForms.Add("CableTrayRoot", form6);
			this.SubForms.Add("CableTrayFitting", form6);
            System.Windows.Forms.Form form7 = new DlgLightning();
			form7.MdiParent = this;
			form7.Parent = this.panel;
			form7.Tag = this;
			this.SubForms.Add("LightningRoot", form7);
			this.SubForms.Add("LightningLine", form7);
		}

		private TreeNode GetTreeNode(string key)
		{
			bool flag = this.treeView.Nodes.ContainsKey(key);
			TreeNode result;
			if (flag)
			{
				result = this.treeView.Nodes[key];
			}
			else
			{
				foreach (object obj in this.treeView.Nodes)
				{
					TreeNode node = (TreeNode)obj;
					TreeNode treeNode = this.GetTreeNode(key, node);
					bool flag2 = treeNode != null;
					if (flag2)
					{
						return treeNode;
					}
				}
				result = null;
			}
			return result;
		}

		private TreeNode GetTreeNode(string key, TreeNode node)
		{
			bool flag = node == null;
			TreeNode result;
			if (flag)
			{
				result = null;
			}
			else
			{
				bool flag2 = node.GetNodeCount(false) > 0;
				if (flag2)
				{
					bool flag3 = node.Nodes.ContainsKey(key);
					if (flag3)
					{
						return node.Nodes[key];
					}
					for (TreeNode treeNode = node.FirstNode; treeNode != null; treeNode = treeNode.NextNode)
					{
						TreeNode treeNode2 = this.GetTreeNode(key, treeNode);
						bool flag4 = treeNode2 != null;
						if (flag4)
						{
							return treeNode2;
						}
					}
				}
				result = null;
			}
			return result;
		}

		private void SelectTreeNode()
		{
			TreeNode treeNode = this.GetTreeNode(this.NodeKey);
			this.treeView.SelectedNode = treeNode;
		}

		private void ShowItemForm()
		{
			TreeNode selectedNode = this.treeView.SelectedNode;
			bool flag = selectedNode == null;
			if (!flag)
			{
				bool flag2 = !this.SubForms.ContainsKey(selectedNode.Name);
				if (!flag2)
				{
                    System.Windows.Forms.Form form = this.SubForms[selectedNode.Name];
					bool flag3 = form != null;
					if (flag3)
					{
						string name = selectedNode.Name;
						if (!(name == "ConduitFitting"))
						{
							if (name == "WireConduit")
							{
								WireConduitForm wireConduitForm = form as WireConduitForm;
								bool flag4 = wireConduitForm != null;
								if (flag4)
								{
									wireConduitForm.UpdateFormData();
								}
							}
						}
						else
						{
							ConduitTypeForm conduitTypeForm = form as ConduitTypeForm;
							bool flag5 = conduitTypeForm != null;
							if (flag5)
							{
								conduitTypeForm.UpdateFormData();
							}
						}
						form.Show();
						form.BringToFront();
					}
				}
			}
		}

		private void treeView_BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
		}

		private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
		{
			this.ShowItemForm();
		}

		private void buttonApply_Click(object sender, EventArgs e)
		{
            foreach (KeyValuePair<string, System.Windows.Forms.Form> keyValuePair in this.SubForms)
			{
				bool flag = keyValuePair.Key == "WireRoot" || keyValuePair.Key == "WireType";
				IElectricSettingItem electricSettingItem;
				if (flag)
				{
					electricSettingItem = (keyValuePair.Value as WireSizeForm);
				}
				else
				{
					bool flag2 = keyValuePair.Key == "CableManage";
					if (flag2)
					{
						electricSettingItem = (keyValuePair.Value as CableManagerForm);
					}
					else
					{
						bool flag3 = keyValuePair.Key == "ConduitRoot" || keyValuePair.Key == "ConduitType";
						if (flag3)
						{
							electricSettingItem = (keyValuePair.Value as ConduitSizeForm);
						}
						else
						{
							bool flag4 = keyValuePair.Key == "ConduitFitting";
							if (flag4)
							{
								electricSettingItem = (keyValuePair.Value as ConduitTypeForm);
							}
							else
							{
								bool flag5 = keyValuePair.Key == "WireConduit";
								if (flag5)
								{
									electricSettingItem = (keyValuePair.Value as WireConduitForm);
								}
								else
								{
									bool flag6 = keyValuePair.Key == "CableTrayRoot" || keyValuePair.Key == "CableTrayFitting";
									if (flag6)
									{
										electricSettingItem = (keyValuePair.Value as CableTrayFittingSet);
									}
									else
									{
										bool flag7 = keyValuePair.Key == "LightningRoot" || keyValuePair.Key == "LightningLine";
										if (flag7)
										{
											electricSettingItem = (keyValuePair.Value as DlgLightning);
										}
										else
										{
											electricSettingItem = null;
										}
									}
								}
							}
						}
					}
				}
				bool flag8 = electricSettingItem != null && electricSettingItem.IsEdited;
				if (flag8)
				{
					electricSettingItem.SaveElectricSettingData();
					electricSettingItem.IsEdited = false;
				}
			}
			this.buttonApply.Enabled = false;
		}

		private void buttonOk_Click(object sender, EventArgs e)
		{
			this.buttonApply_Click(null, null);
			base.DialogResult = DialogResult.OK;
			base.Close();
		}

		private void buttonCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.OK;
			base.Close();
		}

		private void HYElectricSettingMainForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			bool flag = ElectricSettingData.WireSettingProcessData != null;
			if (flag)
			{
				ElectricSettingData.WireSettingProcessData.Clear();
				ElectricSettingData.WireSettingProcessData = null;
			}
			bool flag2 = ElectricSettingData.ConduitSettingProcessData != null;
			if (flag2)
			{
				ElectricSettingData.ConduitSettingProcessData.Clear();
				ElectricSettingData.ConduitSettingProcessData = null;
			}
			bool flag3 = ElectricSettingData.wireConduitSettingProcessData != null;
			if (flag3)
			{
				ElectricSettingData.wireConduitSettingProcessData.Clear();
				ElectricSettingData.wireConduitSettingProcessData = null;
			}
		}

		public void ValidApplyButton()
		{
			this.buttonApply.Enabled = true;
		}

		private const string wireRootKey = "WireRoot";

		private const string wireRootName = "配线设置";

		private const string wireTypeKey = "WireType";

		private const string wireTypeName = "导线类型";

		private const string CableManageKey = "CableManage";

		private const string CableManageName = "电缆管理";

		private const string wireFactorRootKey = "WireFactorRoot";

		private const string wireFactorRootName = "导线载流量校正系数";

		private const string etKey = "ET";

		private const string etName = "环境温度";

		private const string loopsKey = "Loops";

		private const string loopsName = "多回路";

		private const string conduitRootKey = "ConduitRoot";

		private const string conduitRootName = "线管设置";

		private const string conduitTypeKey = "ConduitType";

		private const string conduitTypeName = "线管类型";

		private const string conduitFittingKey = "ConduitFitting";

		private const string conduitFittingName = "线管配件";

		private const string wireConduitKey = "WireConduit";

		private const string wireConduitName = "导线穿管";

		private const string cableTrayRootKey = "CableTrayRoot";

		private const string cableTrayRootName = "电缆桥架设置";

		private const string cableTrayFittingKey = "CableTrayFitting";

		private const string cableTrayFittingName = "电缆桥架配件";

		private const string lightningRootKey = "LightningRoot";

		private const string lightningRootName = "防雷接地";

		private const string lightningLineKey = "LightningLine";

		private const string lightningLineName = "防雷接地线";

		private Document Doc;

		private string NodeKey;

        private Dictionary<string, System.Windows.Forms.Form> SubForms;
	}
}
